var self;
mui.init({
	beforeback: function() {
		if(myVar) {
			clearInterval(myVar);
		}
		var parent = plus.webview.currentWebview().opener();
		mui.fire(parent, "reload");
	}
});
var myVar;
var currenttime;
mui.plusReady(function() {
	self = plus.webview.currentWebview();
	//initData();
	$(".gcmc").html("桩号: " + self.ZH);
	initData();
	//$("#njbz").html(self.SJNJBZ + "kg/L");
	$("#njbz").html("1.8kg/L");
	$("#SJZC").html(self.SJZC + "m");
	myVar = setInterval(function() {
		initData()
	}, 4000);
	initListener();
	//初始中accessToken，deviceSerial，channelNo ，zhiboID
	initCs();
});

var accessToken = "at.5m2cm7mr5lnbmk6f5iam3f98487t9v7q-7slaie3hdg-0gts6vq-cuot9zcnj"; //
var deviceSerial = "240534926"; //
var channelNo = 1; //
//var url="http://hls01open.ys7.com/openlive/e7cc6d78acf246339b6b3c805dcd6e30.hd.m3u8";//视频地址

var datayt = {
	accessToken: accessToken,
	deviceSerial: deviceSerial,
	channelNo: channelNo,
	speed: 2
}

function initListener() {
	$(".icon_back").on("tap", function() {
		mui.back();
	})
}

var control = function(direction) {
	datayt = {
		accessToken: accessToken,
		deviceSerial: deviceSerial,
		channelNo: channelNo,
		speed: 2
	}
	datayt.direction = parseInt(direction); //控制方式,我传了0,1,2,3,8,9
	//	data.speed = '2'; //转动速度我直接写死了,有0,1,2三个值
	//	data.accessToken = "at.5m2cm7mr5lnbmk6f5iam3f98487t9v7q-7slaie3hdg-0gts6vq-cuot9zcnj";
	//	data.deviceSerial="1";
	ajaxF2("https://open.ys7.com/api/lapp/device/ptz/start", datayt, function(res) {
		if(res.code == 200) {
			mui.toast("操作成功")
			stop();
		} else if(res.code == 10002) {
			mui.toast("token过期")
			getRemoteToken();
		}
	}, function(res) {
		console.log(JSON.stringify(res))

	})

}

function getRemoteToken() {
	var param = {
		appKey: "2f740be8db884cfca2e6eaa5c3aafd26",
		appSecret: "75c4fb3d47a48328b774d78aebbd21e5"
	}
	ajaxF2("https://open.ys7.com/api/lapp/token/get", param, function(res) {
		if(res.code == 200) {
			accessToken = res.data.accessToken;
			console.log(accessToken)
			updateToken(accessToken);

		}
	}, function(res) {
		console.log(JSON.stringify(res))

	})
}
// 设置Token
function updateToken(token) {
	ajaxF1(UPDATETOKEN, {
		accesstoken: token,
		jbzid: self.guid
	}, function(result) {

	}, function() {
		mui.toast("token更新失败");
	});

}

//停止转动
var stop = function() {
	datayt = {
		accessToken: accessToken,
		deviceSerial: deviceSerial,
		channelNo: channelNo,
		speed: 2
	}
	ajaxF2("https://open.ys7.com/api/lapp/device/ptz/stop", datayt, function(res) {
		if(res.code == 200) {
			//			decoder = new EZUIKit.EZUIPlayer({
			//				id: 'myPlayer',
			//				autoplay: true,
			//				url: "ezopen://open.ys7.com/" + deviceSerial + "/1.hd.live",
			//				accessToken: accessToken,
			//				decoderPath: 'http://gege.lvjing.site:8080/spjm/js',
			//				width: 600,
			//				height: 400,
			//				handleError: handleError,
			//				handleSuccess: handleSuccess,
			//			});
			//			decoder.openSound();
		}
	})

}

var decoder;

//jianyantoken 是否过期过期updatToken
var stop1 = function() {
	datayt = {
		accessToken: accessToken,
		deviceSerial: deviceSerial,
		channelNo: channelNo,
		speed: 2
	}
	ajaxF2("https://open.ys7.com/api/lapp/device/ptz/stop", datayt, function(res) {
		if(res.code == 200) {
			decoder = new EZUIKit.EZUIPlayer({
				id: 'myPlayer',
				autoplay: true,
				url: "ezopen://open.ys7.com/" + deviceSerial + "/1.hd.live",
				accessToken: accessToken,
				decoderPath: 'http://gege.lvjing.site:8080/spjm/js',
				width: 600,
				height: 400,
			});
			decoder.openSound();
		} else if(res.code == 10002) {
			getRemoteToken();
		}
	})

}

function initData() {
	//plus.nativeUI.showWaiting();
	ajaxF1(GETSGZT, {
		id: self.guid
	}, function(result) {
		if(result.successful) {
			data = result.resultValue;
			if(data.length > 0) {
				var c = 0; //累计质量流差
				var d = 0; //开始质量流差
				if(data.length > 1) {
					$(".dqsnbz").html(data[1].MIDU);
					$(".dqsnjll").html(data[1].LIULIANG);
					if(data[1].ZT == "2") {
						mui.back();
					}
					c = parseFloat(data[1].ACCUMULATE_QUALITY) || 0;
					d = parseFloat(data[1].QSLL) || 0;
				}
				$(".dqjcsd").html(data[0].DOWNUPSPEED);
				$(".dqsnzc").html(data[0].ZDSD);
				if(data[1].MIDU < 1.5 || data[1].MIDU > 2.2) {
					$(".dqsnbz").css("color", "red");
				} else {
					$(".dqsnbz").css("color", "#3399FF");
				}
				if(data[0].ZDSD > self.SJZC) {
					$(".dqsnzc").css("color", "red");
				} else {
					$(".dqsnzc").css("color", "#3399FF");

				}
				if(data[1].MIDU == null) {
					data[1].MIDU = 0;
				}
				var b = data[0].ZDSD || 0;
				//现在时间采集的最大时间
				var shb = getSHB();
				var temp = parseFloat(data[1].MIDU).toFixed(2) + "";
				var zsmd = shb[temp];
				var zxz = parseFloat(Object.keys(shb)[0]);
				var zdz = parseFloat(Object.keys(shb)[Object.keys(shb).length - 1]);
				var lsz = parseFloat(data[1].MIDU).toFixed(2);
				if(zsmd == null) {
					if(lsz < zxz) {
						zsmd = Object.keys(shb)[0];
					} else if(lsz > zdz) {
						zsmd = Object.keys(shb)[Object.keys(shb).length - 1]
					}
				} else {
					zsmd = temp;
				}
				if(b > 0 && lsz > 0) {
					var ss = ((c - d) * 1000 / b) * parseFloat(shb[zsmd]);
					$(".mmpjsnl").html(ss.toFixed(2));
				} else if(b <= 0) {
					$(".mmpjsnl").html(0)
				}

			}
		}

		//plus.nativeUI.closeWaiting();
	}, function() {
		mui.toast("请求失败");
		//plus.nativeUI.closeWaiting();
	});

}

function initCs() {
	//初始化参数;
	try {
		ajaxF1(GETSXT, {
			// 改为施工处ID字段名字保持不变
			jbzid: self.SGCID
		}, function(result) {
			if(result.successful) {
				data = result.resultValue;
				if(data.length > 0) {
					accessToken = data[0].ACCESSTOKEN;
					deviceSerial = data[0].DEVICESERIAL;
					channelNo = data[0].CHANNELNO;
					var ZBID = data[0].ZHIBOID; // 直播ID
					var rtmp = 'rtmp://rtmp01open.ys7.com/openlive/' + ZBID + ".hd";
					var hls = "http://hls01open.ys7.com/openlive/" + ZBID + ".hd.m3u8";
					$("#myPlayer").append('<source id="rtmp_video"  src="' + rtmp + '"  type="" />');
					$("#myPlayer").append('<source  id="hls_video"   src="' + hls + '"   type="application/x-mpegURL" />');
					//初始化直播声音
					//					stop1();
					//decoder.closeSound();

				}
			}
		}, function() {
			mui.toast("请求失败");

		});
	} catch(e) {

	}
}

//获取分种数
function DateDifference(startime, endtime) {
	// let d1 = new Date(faultDate);
	// let d2 = new Date(completeTime);
	var stime = new Date(startime).getTime();
	var etime = new Date(endtime).getTime();
	var usedTime = etime - stime; //两个时间戳相差的毫秒数
	//计算出小时数
	var minutes = Math.floor(usedTime / (60 * 1000));
	return minutes;
}